package com.llp.declaresystem.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llp.declaresystem.domain.dto.HighQueryProjectDto;
import com.llp.declaresystem.domain.dto.ProjectDto;
import com.llp.declaresystem.domain.dto.UserDto;
import com.llp.declaresystem.domain.entity.*;
import com.llp.declaresystem.mapper.*;
import com.llp.declaresystem.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author NASK
* @description 针对表【unit(单位信息表)】的数据库操作Service实现
* @createDate 2022-11-23 16:22:50
*/
@Service
public class UnitServiceImpl extends ServiceImpl<UnitMapper, Unit>
    implements UnitService{

    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProjectCategoryMapper projectCategoryMapper;
    @Autowired
    private CategoryUnitMapper categoryUnitMapper;

    /**
     * 根据uid查询单位信息
     * @param uid
     * @return
     */
    @Override
    public Unit selectUnitInfoByUid(Integer uid) {
        Unit unit = unitMapper.selectByUid(uid);
        return unit;
    }

    /**
     * 根据uid进行更新操作
     * @param unit
     * @return
     */
    @Override
    public int updateByUid(Unit unit) {
        int res = unitMapper.updateByUid(unit);
        return res;
    }
    /**
     * 根据uid修改用户密码
     * @param userDto
     * @return
     */
    @Override
    public int updatePwdByUid(UserDto userDto) {
        User user = userMapper.selectByUid(userDto.getUid());
        System.out.println("user:"+user);
        if (user != null) {
            if (StringUtils.equals(user.getPassword(), userDto.getOldPassword())) {
                int res = userMapper.updatePwdByUid(userDto);
                System.out.println("res:"+res);
                return res;
            } else {
                return -1;
            }
        }
        return 0;
    }
    /**
     * 根据expertId去查找项目信息
     * @param categoryId
     * @return
     */
    @Override
    public Project selectProjectByUidAndCid(Integer categoryId) {
        Project project = projectMapper.selectProjectByCategoryId(categoryId);
        return project;
    }
    /**
     * 返回项目类别信息
     * @param uid
     * @return
     */
    @Override
    public List<ProjectCategory> selectAllProjectCategory(Integer uid) {
        Unit unit = unitMapper.selectByUid(uid);
        if(unit!=null){
            List<ProjectCategory> projectCategorys = new ArrayList<>();
            Integer unitid = unit.getUnitid();
            List<Integer> integers = categoryUnitMapper.selectByUnitid(unitid);
            for (int i = 0; i < integers.size(); i++) {
                ProjectCategory projectCategory = projectCategoryMapper.selectByCategoryid(integers.get(i));
                projectCategorys.add(projectCategory);
            }
            return projectCategorys;
        }else{
            return null;
        }
    }
    /**
     * 更新项目信息
     * @param project
     * @return
     */
    @Override
    public int updateProjectInfo(Project project) {
        Project project1 = projectMapper.selectAllByProjectId(project.getProjectId());
        if(project1!=null){
            int res = projectMapper.updateProjectByPid(project);
            return res;
        }
        return -1;
    }

    /**
     * 根据categoryId查询项目信息
     * @param categoryId
     * @return
     */
    @Override
    public ProjectCategory selectProjectCategory(Integer categoryId) {
        ProjectCategory projectCategory = projectCategoryMapper.selectByCategoryid(categoryId);
        return projectCategory;
    }
    /**
     * 新增项目信息
     * @param project
     * @return
     */
    @Override
    public int insertProject(Project project) {
        return projectMapper.insertProjectByCategoryid(project);
    }

    /**
     * 根据项目内容进行简单查询
     * @param projectContent
     * @return
     */
    @Override
    public Set<ProjectDto> selectByProjectContent(String projectContent) {
        //set集合去重
        Set<ProjectDto> allProjects = new HashSet<>();
        //根据用户输入信息检索主项目检索出所有categoryid
        List<Integer> categories = projectCategoryMapper.selectByCategoryname(projectContent);
        for (int i = 0; i < categories.size(); i++) {
            //从项目表中检索出项目信息存入set集合中
            List<ProjectDto> project = projectMapper.selectProjectInfoByCategoryId(categories.get(i));
            allProjects.addAll(project);
        }
        //从分项目中检索出来的所有项目信息存入set集合中
        List<ProjectDto> projects1 = projectMapper.selectByProjectName(projectContent);
        allProjects.addAll(projects1);
        return allProjects;
    }

    /**
     * 根据projceid查询项目信息
     * @param pid
     * @return
     */
    @Override
    public Project selectProjectByPid(Integer pid) {
        Project project = projectMapper.selectByPid(pid);
        return project;
    }

    /**
     * 高级查询
     * 存在bug
     * @param highQueryProjectDto
     * @return
     */
    @Override
    public Set<HighQueryProjectDto> highQueryProject(HighQueryProjectDto highQueryProjectDto) {
        Set<HighQueryProjectDto> projects = new HashSet<>();
        List<Integer> categorys = projectCategoryMapper.selectByCategorynameAndRepStartTimeAndRepStopTime(highQueryProjectDto);
        for (int i = 0; i < categorys.size(); i++) {
            List<HighQueryProjectDto> highQueryProjectDtos = projectMapper.selectByCategoryidAndCapital(categorys.get(i), highQueryProjectDto.getCapital());
            projects.addAll(highQueryProjectDtos);
        }
        return projects;
    }

    /**
     * 根据id删除对应的单位信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteUnitById(Integer id) {
        unitMapper.deleteById(id);
        return true;
    }

}




